package com.sinothk.gpserver.modules.biz.controller;

import com.sinothk.gpserver.modules.comm.base.controller.BaseController;
import com.sinothk.gpserver.modules.biz.entity.RoomCardLogEntity;
import com.sinothk.gpserver.modules.biz.service.IRoomCardLogService;
import com.sinothk.gpserver.modules.system.entity.LoginEntity;
import com.sinothk.server.support.annotation.callback.BizException;
import com.sinothk.server.support.annotation.callback.ResponseResult;
import com.sinothk.server.support.entity.PageData;
import com.sinothk.server.support.entity.PageVo;
import com.sinothk.server.support.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 1. 好友邀请功能
 * </p>
 *
 * @author Liang Y.t.
 * @since 2021-09-13
 */
@Api(tags = "房卡发放功能")
@RequestMapping("/roomCardLog")
@ResponseResult()
@RestController
public class RoomCardLogController extends BaseController {

    @Resource
    private IRoomCardLogService roomCardLogService;

    @ApiOperation(value = "分页列表", notes = "分页列表")
    @PostMapping("/page")
    public PageData<RoomCardLogEntity> page(@RequestBody PageVo<RoomCardLogEntity> pageVo) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        pageVo.getData().setSetAccount(login.getAccount());
        return roomCardLogService.page(pageVo);
    }

    @ApiOperation(value = "详情", notes = "详情")
    @GetMapping("/get/{id}")
    public RoomCardLogEntity get(@ApiParam("id") @PathVariable String id) {
        return roomCardLogService.getById(id);
    }

//    @ApiOperation(value = "新增", notes = "新增")
//    @PostMapping(value = "/add")
//    public String add(@RequestBody RoomCardLogEntity roomCardLogEntity) {
//        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
//        roomCardLogEntity.setSetAccount(login.getAccount());
//
//        roomCardLogEntity.setCreateBy(login.getAccount());
//        roomCardLogService.save(roomCardLogEntity);
//        return "新增成功";
//    }

    @ApiOperation(value = "充值", notes = "充值")
    @PostMapping(value = "/addLog")
    public String addLog(@RequestParam("getAccount") String getAccount, @RequestParam("value") String value) {
        roomCardLogService.addLog(getAccount, value);
        return "新增成功";
    }


    @ApiOperation(value = "修改", notes = "修改")
    @PutMapping("/update")
    public RoomCardLogEntity update(@RequestBody RoomCardLogEntity roomCardLogEntity) {
        if (StringUtil.isEmpty(roomCardLogEntity.getId())) {
            throw new BizException("id不能为空");
        }
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        roomCardLogEntity.setUpdateBy(login.getUserName());
        roomCardLogService.updateById(roomCardLogEntity);
        return roomCardLogService.getById(roomCardLogEntity.getId());
    }

    @ApiOperation(value = "新增/更新", notes = "新增/更新")
    @PostMapping(value = "/saveOrUpdate")
    public String saveOrUpdate(@RequestBody RoomCardLogEntity roomCardLogEntity) {
        LoginEntity login = (LoginEntity) SecurityUtils.getSubject().getPrincipal();
        if (StringUtil.isEmpty(roomCardLogEntity.getId())) {
            roomCardLogEntity.setCreateBy(login.getAccount());
            roomCardLogService.save(roomCardLogEntity);
            return "新增成功";
        } else {
            roomCardLogEntity.setUpdateBy(login.getUserName());
            roomCardLogService.updateById(roomCardLogEntity);
            return "更新成功";
        }
    }

    @ApiOperation(value = "删除（逻辑删除）", notes = "删除（逻辑删除）")
    @DeleteMapping("/del/{id}")
    public String del(@ApiParam("id") @PathVariable String id) {
        roomCardLogService.removeById(id);
        return "已逻辑删除";
    }
}
